//JavaScript Framework 2.0 Code
try{
Type.registerNamespace('com.yonyou.ncmm_pac_pda.PoArrivalOrderSearchController');
com.yonyou.ncmm_pac_pda.PoArrivalOrderSearchController = function() {
    com.yonyou.ncmm_pac_pda.PoArrivalOrderSearchController.initializeBase(this);
    this.initialize();
}
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$initialize(){
    //you can programing by $ctx API
    //get the context data through $ctx.get()
    //set the context data through $ctx.push(json)
    //set the field of the context through $ctx.put(fieldName, fieldValue)
    //get the parameter of the context through $ctx.param(parameterName)
    //Demo Code:
    //    var str = $ctx.getString();      //获取当前Context对应的字符串
    //    alert($ctx.getString())          //alert当前Context对应的字符串
    //    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
    //    json["x"] = "a";        //为当前json增加字段
    //    json["y"] = [];           //为当前json增加数组
    //    $ctx.push(json);            //设置context，并自动调用数据绑定
    //    
    //    put方法需手动调用databind()
    //    var x = $ctx.get("x");    //获取x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
    //    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
    //    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
    //    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组
    
    //your initialize code below...
    
}
    
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$evaljs(js){
    eval(js)
}

var PrintData;
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$imagebutton0_onclick(sender, args){
	$view.open({
		viewid : "com.yonyou.ncmm_pac_pda.PoArriveQuery",
		isKeep : "true",
		callback : "doQuery()"
	})
}
function doQuery(){
	var cdon = $param.getJSONObject("qcondition");
	var json = {};
	json = cdon;
	//$alert(json);
	loadData(json);
}
function onEventClick_ListViewItemClick(sender, args){
	var index = $id("listviewdefine0").get("rowindex");
	//var json = PrintData["listdata"][index];
	//$alert(json);
	//$alert("点击了第"+index+"项");
	$view.open({
		viewid : "com.yonyou.ncmm_pac_pda.PoArrivalOrder",
		isKeep : "true",
		"data": PrintData,
		"index":index,
		"size":$cache.read("size"),
		callback : "doPrint()"
	});
}
function doPrint(){
	
}

/**
	 * 从NC端获取采购到货单数据
	 * 清空本地数据表
	 * 从本地数据库中读取采购到货单数据并显示在页面上
	 */
	function loadData(condition) {
	//获取默认组织，如果默认组织为空，提示
		var stororg = $stringToJSON($cache.read("defaultstockorg"));
		if(stororg == null || stororg == "") {
			$toast("下载前请先进行业务设置！");
			return;
		}
		//收货库存组织
		condition["pk_arrvstoorg"] = "='" + stororg.pk_stockorg + "'";
		var stordoc = $stringToJSON($cache.read("defaultstordoc"));
		if(stordoc != null && stordoc != "") {
		//收货仓库
		condition["pk_recvstordoc"] ="in('"+stordoc.pk_stordoc+"','~')";
		}
	   //取默认仓库，如果有值，收货仓库为空或等于默认仓库  
		var vocount = 0;
		NCMMDBUtil.testNCConnection(function(data) {
			if (data == false || data == "false") {
				$js.hideLoadingBar();
				$toast("下载失败请检查网络设置");
				return;
			} else {
				$app.showLoadingBar({
					msg: "正在下载……"
				});
				//$alert($jsonToString(condition));
				//1. 从NC端获7取档案数据
				NCMMDBUtil.loadNCBaseData(PoArriveVO, PoArriveVO, NCMMHttpUtil.BILL, condition, function(data) {
					//如果没有从NC端下载到数据，则本地记录不更新
					//$alert($jsonToString(data));
					if (data == null) {
						$js.hideLoadingBar();
						$toast("没有获取到符合条件的订单");
						return;
					}
					if (data != null && typeof (data) == 'string') {
						$js.hideLoadingBar();
						$toast("下载失败：" + data);
						return;
					}

					//$alert("process data=" + $jsonToString(data));
					// 如果没有信息直接返回
					var billinfos = data.billinfos || {};
					var props = Object.getOwnPropertyNames(billinfos) || [];
					if (props.length == 0) {
						if ($isJSONObject(data)) {
							$js.hideLoadingBar();
							$toast("没有下载采购订单");
						} else {
							$js.hideLoadingBar();
							$toast(data);
						}
						return;
					}
					var unUpdateFieldanmes = new Array();
					var numAndUnitJson = {
						num : "nnum",
						cunitid : "cunitid"

					};
					var astnumAndUnitJson = {
						num : "nastnum",
						cunitid : "castunitid"

					};
					vocount = NCMMLOADVOUTIL.updateLocalVosByNC(PoArriveVO, billinfos, "scanednum", numAndUnitJson, astnumAndUnitJson, unUpdateFieldanmes);
					if (!vocount || vocount < 1) {
						$js.hideLoadingBar();
						$toast("下载的数据为空，请重新检查");
						return 0;
					}

					loadDataFromLocalDB();
					$js.hideLoadingBar();
					$toast("采购订单下载了【" + vocount + "】条数据。");
					$cache.write("size",vocount);
				});
				//3 从本地数据库中读取数据
				return vocount;
			}
		});
	}

function loadDataFromLocalDB() {
		if (NCMMPubUtil.isTableExist(PoArriveVO) == false) {
			return;
		}
		NCMMPubUtil.queryVOByConditions(PoArriveVO, null, function(data) {
		
			if (NCMMPubUtil.isEmpty(data)) {
				$toast("本地没有订单记录！");
				//return;
			}
			//$alert(data);
			data=eval(data);
			//$alert(data);
			//特殊字符处理
			data = NCMMPubUtil.htmlspecialjsonarray2UI(data);
			//$alert(data);
			//$alert("本地data="+$jsonToString(data));
			var jsondata = {
				puinlist : eval(data)
			};
			//$alert("下载到的数据为:"+jsondata);
			PrintData = data;
			$ctx.push(jsondata);
			$ctx.dataBind();
		});
		//NCMMPubUtil.notifyFilter("search0", "listviewdefine0", ["vbillcode", "materialname"]);
	}

/**
* 插入主子表数据
*/

	function getPkOrg(condition) {
		var pk_org = NCMMPUINUtil.getDefaultStockPK();
		if (pk_org == null || pk_org == "") {
			return false;
		}
		pk_org = "='" + pk_org + "'";
		condition = $.extend({}, {
			"pk_org" : pk_org
		}, condition);
		return condition;
	}

/**
* 从NC中获取本日采购到货单
* @param {Object} sender
* @param {Object} args
*/
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$getTodayData(sender, args){
	
	var daydate = NCMMDateUtil.getDayDate();
		var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
		var condition = {
			"dbilldate" : dbilldate
		};
		condition = getPkOrg(condition);
		if (condition == false) {
			$toast("下载前请先进行业务设置");
			return;
		}
		loadData(condition);
}
/**
* 从NC中获取本周采购到货单
* @param {Object} sender
* @param {Object} args
*/
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$getWeekData(sender, args){
	var weekDate = NCMMDateUtil.getWeekDate();
		var dbilldate = "between '" + weekDate.beginDate + "' and '" + weekDate.endDate + "'";
		var condition = {
			"dbilldate" : dbilldate
		};
		condition = getPkOrg(condition);
		if (condition == false) {
			$toast("下载前请先进行业务设置");
			return;
		}
		loadData(condition);
}
/**
* 从NC中获取本月采购到货单
* @param {Object} sender
* @param {Object} args
*/
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$getMonthData(sender, args){
	var monthDate = NCMMDateUtil.getMonthDate();
		var dbilldate = "between '" + monthDate.beginDate + "' and '" + monthDate.endDate + "'";
		var condition = {
			"dbilldate" : dbilldate
		};
		condition = getPkOrg(condition);
		if (condition == false) {
			$toast("下载前请先进行业务设置");
			return;
		}
		loadData(condition);
}
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$onload(sender, args){
	loadDataFromLocalDB();
}
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$menuButtonListener(sender, args){
	//$alert("success");
	NCMMPUINUtil.showMenuLists("btn_menu");
}
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$CreateTable(sender, args){
	if ($cache.read("isCreatePoTable") == "true")
			return;
		NCMMPubUtil.dropTable(PoArriveVO);
		NCMMPubUtil.createTable(PoArriveVO);

		$cache.write("isCreatePoTable", "true");
		NCMMPUINTEST.initTable(PoArriveVO);
		if ($cache.read("isCreatePoTable") == "true")
			return;
		NCMMPubUtil.dropTable(PoArriveScanVO);
		NCMMPubUtil.createTable(PoArriveScanVO);

		$cache.write("isCreatePoTable", "true");
}
function com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$button0_onclick(sender, args){
	$view.close({
        "resultCode": "15"//当resultCode为15时，可以响应父页面的callback
    });
}
com.yonyou.ncmm_pac_pda.PoArrivalOrderSearchController.prototype = {
    button0_onclick : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$button0_onclick,
    CreateTable : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$CreateTable,
    menuButtonListener : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$menuButtonListener,
    onload : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$onload,
    getTodayData : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$getTodayData,
    getWeekData : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$getWeekData,
    getMonthData : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$getMonthData,
    imagebutton0_onclick : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$imagebutton0_onclick,
    initialize : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$initialize,
    evaljs : com$yonyou$ncmm_pac_pda$PoArrivalOrderSearchController$evaljs
};
com.yonyou.ncmm_pac_pda.PoArrivalOrderSearchController.registerClass('com.yonyou.ncmm_pac_pda.PoArrivalOrderSearchController',UMP.UI.Mvc.Controller);
}catch(e){$e(e);}
